home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_08 / plauger / xstod.c < prev   
C/C++ Source or Header  |  1994-06-09  |  591b  |  23 lines

  1. -------------------------- Listing x: simple _Stod function ---------
  2.  
  3. // xstod -- _Stod LIMITED SUBSTITUTE
  4. #include <errno.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <istream>
  8.  
  9. double _Stod(const char *s, char **ep, long pten)
  10.     {    // convert text to double and scale
  11.     double x = strtod(s, ep);
  12.     if (x != 0 && pten != 0)
  13.         {    // scale the result
  14.         while (pten < 0)
  15.             x /= 10, ++ pten;    // MAY MISBEHAVE
  16.         while (0 < pten)
  17.             x *= 10, --pten;    // MAY MISBEHAVE
  18.         if (x == 0 || x == HUGE_VAL || x == -HUGE_VAL)
  19.             errno = ERANGE;    // MAY MISBEHAVE
  20.         }
  21.     return (x);
  22.     }
  23.